/** Table Model based on JDBC  2.0 :

    An JTable model is defined for a SQL statement. This table model used some
features coming with JDBC 2.0 driver.

*/

import java.sql.*;
import java.util.*;
import javax.swing.table.*;

class JDBC2TableModel extends AbstractTableModel {

    JDBC2TableModel() {
	// For direct access to cheval.
	String	url = "jdbc:oracle:thin:@cheval.cs.csubak.edu:1521:TEST";

	// For access cheval from applet on pegasus.
	String user="cs440", passwd="student1";
	// String url = "jdbc:oracle:thin:cs440/student1@(description=(address_list=(address=(protocol=tcp)(host=pegasus.cs.csubak.edu)(port=1630))(address=(protocol=tcp)(host=cheval.cs.csubak.edu)(port=1521)))(source_route=yes)(connect_data=(sid=TEST)))";
	try {
		//Class.forName("oracle.jdbc.driver.OracleDriver");
		DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver() );
		cnn = DriverManager.getConnection(url, "cs440", "student1");
		cnn.setAutoCommit(true); 
		stmt = cnn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
		getResultSet(sql);
   	} catch (SQLException e ) { e.printStackTrace(); System.exit(-1); }
   	 // catch (ClassNotFoundException e2 ) { e2.printStackTrace(); System.exit(-1); }
    }

    void getResultSet( String sql ) {
	try {
		resultSet = stmt.executeQuery(sql);
		getColumnTitles();
		resultSet.last();
		recordCount = resultSet.getRow();
		resultSet.first();
   	} catch (SQLException e ) { e.printStackTrace(); }
    }

    void getColumnTitles() {
	try { 
		ResultSetMetaData meta = resultSet.getMetaData();
		columnCount = meta.getColumnCount();
		columnTitles = new Vector(columnCount);
		for ( int i = 1; i <= columnCount; i ++ ) 
			columnTitles.add(ScreenIO.expandString(meta.getColumnName(i), 15));
	} catch( SQLException e) { e.printStackTrace(); }
    }

    public boolean isCellEditable(int row, int col) { return true; }
    public int getRowCount()    { return recordCount; }
    public int getColumnCount() { return columnCount;}
    public String getColumnName(int col) { return (String) columnTitles.elementAt(col); }
	
    public Object getValueAt(int row, int col) {
	try {
		// resultSet.absolute(row + 1);
		resultSet.relative (row - currentRow); currentRow = row;

		return resultSet.getString(col+1) ;
	} catch ( SQLException e ) { e.printStackTrace() ; }
	return "";
    }
    public void  setValueAt( Object obj, int row, int col ) {
	try {
		// resultSet.absolute(row + 1);
		resultSet.relative (row - currentRow); currentRow = row;
		//System.out.println("Current row: " + resultSet.getRow() + ", New value: " + obj );
		resultSet.updateString(col+1, (String) obj);
		resultSet.updateRow();
		cnn.commit();
	} catch (SQLException e) { e.printStackTrace(); }
    }

    public void addRow( Object obj ) {
    }

    public Object removeRow( int row ) {
	/*
	try {
	} catch (SQLException e ) { e.printStackTrace() ; }
	*/
	return "Hello";
    }
    public void saveData() {
	try { cnn.commit(); } catch (SQLException e) { e.printStackTrace(); }
    }

    public void requry(String name) {
	String tmp = sql + " WHERE lastName like '" + name + "%' OR firstName like '" + name + "%'" ;
	System.out.println(tmp);
	getResultSet(tmp);
	fireTableDataChanged();
	currentRow = 0;
    }

    // Data members
    String sql = "SELECT LastName, FirstName, Phone1 \"Home No.\", Phone2 \"Work No.\" FROM AddressBook ";
    Connection	cnn;
    Statement	stmt;
    ResultSet	resultSet;
    Vector	columnTitles;
    int currentRow = 0, columnCount = 0, recordCount = 0;
  
}

